package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.BaseOtherElfBean;
import com.koron.css2.baseConfig.mapper.BaseOtherElfMapper;
import com.koron.css2.baseConfig.vo.BaseOtherElfVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
/**
 * 2020-06-11
 * @author 10730
 *
 */
public class BaseOtherElfAdd implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		// TODO Auto-generated method stub
		
		try {
			BaseOtherElfVO bean = JsonUtils.objectToPojo(req.getData(), BaseOtherElfVO.class);
			BaseOtherElfMapper mapper = factory.getMapper(BaseOtherElfMapper.class);

			bean.setNoticeStaff1(bean.getNoticeStaff1().replace("，", ","));
			bean.setNoticeStaff2(bean.getNoticeStaff2().replace("，", ","));
			bean.setNoticeStaff3(bean.getNoticeStaff3().replace("，", ","));
			bean.setNoticeStaff4(bean.getNoticeStaff4().replace("，", ","));
			
			String[] staff1=bean.getNoticeStaff1().split(","),
					staff2=bean.getNoticeStaff2().split(","),
					staff3=bean.getNoticeStaff3().split(","),
					staff4=bean.getNoticeStaff4().split(",");
			List<BaseOtherElfBean> list = new ArrayList<BaseOtherElfBean>();
			for (String str : staff1) {
				list.add(new BaseOtherElfBean("1",str,userInfo));
			}
			for (String str : staff2) {
				list.add(new BaseOtherElfBean("2",str,userInfo));
			}
			for (String str : staff3) {
				list.add(new BaseOtherElfBean("3",str,userInfo));
			}
			for (String str : staff4) {
				list.add(new BaseOtherElfBean("4",str,userInfo));
			}
			mapper.delBaseOtherElf();
			mapper.addBaseOtherElf(list);
		} catch (Exception e) {
			 factory.close(false);
	         logger.error("其它精灵保存失败", e);
	         return MessageBean.create(Constant.MESSAGE_INT_FAIL, "其它精灵保存失败", void.class);
		}
		return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
	}

}
